package org.neptune.service.service.impl;

import java.util.List;
import java.util.Set;

import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;
import org.neptune.api.entity.*;
import org.neptune.api.request.SeckillRequest;
import org.neptune.common.constant.CharacterConstant;
import org.neptune.common.constant.NeptuneConstant;
import org.neptune.common.enums.SeckillStatus;
import org.neptune.common.response.ResponseBean;
import org.neptune.service.dao.SeckillLogMapper;
import org.neptune.service.dao.SeckillOrderWaitPayMapper;
import org.neptune.service.redis.RedisRepository;
import org.neptune.service.redis.RedissonUtil;
import org.neptune.service.service.SeckillOrderService;
import org.neptune.service.util.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author lwl
 * @date 2020/4/2 11:35
 */
@Service
public class SeckillOrderServiceImpl implements SeckillOrderService {

	@Autowired
	private RedisRepository redisRepository;

	@Autowired
	private SeckillOrderWaitPayMapper seckillOrderWaitPayMapper;

	private SeckillLogMapper seckillLogMapper;

	@Override
	public List<SeckillOrder> querySeckillOrders(SeckillRequest seckillRequest) {
		List<SeckillOrder> seckillOrders = Lists.newArrayList();
		SeckillUser user = WebUtil.getLoginUser();
		if (StringUtils.equals(SeckillStatus.ORDER_STATUS_VALID.getStatus(),
				seckillRequest.getOrderStatus())){
			String keyPattern = RedissonUtil.getRedisKey(NeptuneConstant.REDIS_SECKILL_USER_ORDER,
					user.getId() + CharacterConstant.HYPHEN + CharacterConstant.ASTERISK);;

			Set<String> keys = redisRepository.getKeysByPattern(keyPattern);

			for (String key : keys){
				Long remainTime = redisRepository.ttl(key);
				CacheBean cacheBean = redisRepository.get(key, CacheBean.class);
				if (cacheBean != null){
					SeckillOrder seckillOrder = new SeckillOrder();
					seckillOrder.setSeckillName(this.getSeckillName(cacheBean.getSeckillId()));
					seckillOrder.setSeckillId(cacheBean.getSeckillId());
					seckillOrder.setRemainTime(remainTime.intValue());
					seckillOrders.add(seckillOrder);
				}
			}

		} else{
			// TODO
		}

		return seckillOrders;
	}

	@Override
	public ResponseBean<Object> payOrder(SeckillRequest seckillRequest) {
		SeckillUser user = WebUtil.getLoginUser();
		String cacheUserKey = RedissonUtil.getRedisKey(NeptuneConstant.REDIS_SECKILL_USER_ORDER,
				user.getId() + CharacterConstant.HYPHEN + seckillRequest.getSeckillId());
		CacheBean cacheBean = redisRepository.get(cacheUserKey, CacheBean.class);
		if (cacheBean == null){
			return ResponseBean.fail(null, "订单过期");
		}
		redisRepository.remove(cacheUserKey);

		SeckillLog seckillLog = new SeckillLog();
		SeckillLogExample example = new SeckillLogExample();
		example.createCriteria().andUserIdEqualTo(user.getId()).andSeckillIdEqualTo(seckillRequest.getSeckillId());
		seckillLog.setState(new Byte(SeckillStatus.ORDER_STATUS_PAID.getStatus()));
		seckillLogMapper.updateByExample(seckillLog, example);

		SeckillOrderWaitPayExample seckillOrderWaitPayExample = new SeckillOrderWaitPayExample();
		seckillOrderWaitPayExample.createCriteria().andUserIdEqualTo(user.getId())
				.andSeckillIdEqualTo(seckillRequest.getSeckillId());
		seckillOrderWaitPayMapper.deleteByExample(seckillOrderWaitPayExample);

		return ResponseBean.success(null, "下单成功");
	}

	@Override
	public ResponseBean<Object> cancelOrder(SeckillRequest seckillRequest) {
		SeckillUser user = WebUtil.getLoginUser();
		String cacheUserKey = RedissonUtil.getRedisKey(NeptuneConstant.REDIS_SECKILL_USER_ORDER,
				user.getId() + CharacterConstant.HYPHEN + seckillRequest.getSeckillId());
		redisRepository.remove(cacheUserKey);
		return ResponseBean.success(null, "取消成功");
	}

	private String getSeckillName(String id) {
		String redisKey = RedissonUtil.getRedisKey(NeptuneConstant.REDIS_SECKILL_GOODS_INFO,
				id);
		Seckill seckill = redisRepository.get(redisKey, Seckill.class);
		if (seckill != null){
			return seckill.getName();
		}
		return "";
	}
}
